home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr28 / alogin21.zip / ALOGIN.C next >
C/C++ Source or Header  |  1993-05-07  |  18KB  |  630 lines

  1. /***************************************************************************
  2. *  AutoLogin: a quick and easy login program for LANtastic(R) NOS (LANOS)
  3. *          Copyright 1990, 1993 J. A. Gerring
  4. *            Version 2.1
  5. *
  6. *   This source may not be modified, included in another program,
  7. *   distrubuted with another program, or distributed without its
  8. *   documentation and executable without express written permission
  9. *   of the author.
  10. *
  11. *   LANtastic is a Registered Trademark of Artisoft, Inc.
  12. *
  13. *   I may be contacted via the following methods:
  14. *
  15. *   CompuServe ID:  73650,471  (best method)
  16. *   US MAIL:        5500 N. Valley View #108, Tucson, AZ 85718
  17. *
  18. ****************************************************************************/
  19.  
  20.  
  21. /* includes */
  22. #include <ctype.h>
  23. #include <stdlib.h>
  24. #include <dos.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <conio.h>
  28. #include <time.h>
  29.  
  30. /* LANOS defined device types and misc. other defines */
  31. #define PRINTER     3
  32. #define DRIVE        4
  33. #define MAX_ADAPTERS    6
  34. #define ESC_CHAR    '\x1b'
  35. #define PAUSE_LENGTH    5
  36.  
  37. /* register declartions */
  38. static union REGS rg;
  39. struct SREGS sg;
  40.  
  41. /* Misc. buffers and pointers to buffers */
  42. char far *error, far *eptr;
  43. char err[120], *bptr;
  44. char server[17];
  45. char buf[81], garb[80], WaitFlag;
  46.  
  47. /* file pointer for the AutoLogin data file */
  48. FILE *fp;
  49.  
  50.  
  51. /***************************************************************************
  52. * main()
  53. *
  54. *   Entry point for AutoLogin.    Gets user name and password from user and
  55. *   opens the user's data file.
  56. *
  57. *   Calls: login(), waitfor(), use(), auto().
  58. *
  59. ****************************************************************************/
  60.  
  61. main()
  62. {
  63. char user[17];        /* user name        */
  64. char pswrd[17];     /* password        */
  65. char filename[23];    /* data file name   */
  66. char bool;        /* flag to signal that data has been read from file */
  67. int pos, pos1, ch, n;    /* various variables */
  68.  
  69.  
  70.    /*  Print logo */
  71.    printf("            --- AutoLogin 2.1 for LANtastic(R) ---");
  72.    printf("\n                 (c)1990,1993 by J Gerring\n");
  73.  
  74.    /* check to make sure LANtastic redirector is running */
  75.    if (CheckForLAN() == 0) {
  76.       printf("\n\n\aLANtastic NOS not currently running. Exiting AutoLogin.\n\n");
  77.       exit(2);
  78.       }
  79.  
  80.    WaitFlag=0;        /* clear WaitFlag => we're not in global wait mode */
  81.  
  82.    /* get user name from console */
  83.    memset(user, '\0', 17);
  84.    printf("\nEnter username: ");
  85.    scanf("%s",user);
  86.    towupper(user);
  87.  
  88.    /* get password (a little more complicated that you might think!)  */
  89.    memset(pswrd, '\0', 17);
  90.    printf("password: ");            /* give user a prompt */
  91.    pos = 0;
  92.    ch = getch();          /* this loop reads the password getting   */
  93.    while (1) {              /* characters until enter is hit. It also */
  94.       if (ch == 13)          /* allows the user to backspace and retype*/
  95.      break;           /* if they make a mistake.            */
  96.       if (isgraph(ch)) {
  97.     putchar('*');          /* echo stars for valid input         */
  98.     pswrd[pos++] = ch;
  99.     }
  100.       else if (ch == '\b' && pos != 0) {    /* the backspace function        */
  101.     pswrd[--pos] = '\0';
  102.     printf("\b \b");
  103.     }
  104.       ch = getch();
  105.       }
  106.    pswrd[pos] = '\0';          /* null terminate the password        */
  107.    towupper(pswrd);          /* convert it to upper case            */
  108.  
  109.    /* set up filename, open the user's file */
  110.    strcpy(filename,"\\LANTASTI\\");
  111.    if ((pos = strlen(user)) >= 9) {       /* if username is > 9  use first 4   */
  112.       strncat(filename, user, 4);         /* characters and last 4 characters  */
  113.       strncat(filename, (user+pos-4), 4);
  114.       }
  115.    else
  116.       strcat(filename, user);
  117.    strcat(filename, ".DAT");
  118.    if ((fp = fopen(filename, "r")) == NULL) {
  119.       printf("\n\n\aERROR: Cannot open file - %s... Exiting Login\n\n", filename);
  120.       exit(1);
  121.       }
  122.  
  123.    /* Turn off LANtastic's Autologin feature.  This will make logins and uses faster. */
  124.    /* fake up input buffer for the autolog() function to parse */
  125.    memset(buf, '\0', 81);
  126.    strcat(buf, "AUTO OFF");
  127.    autolog(user,pswrd);          /* call autolog() */
  128.  
  129.    /* read user's data file, take indicated action. */
  130.    printf("\n\nLogging in...\n");
  131.    bool = 0;
  132.    for(;;) {
  133.       /* read line of input from user's file */
  134.       if (!fill_buf()) {
  135.      if (!bool)
  136.         printf("\n\aWarning: no data found in file %s\n",filename);
  137.      printf("\nLogin finished!\n\n");
  138.      fclose(fp);
  139.      exit(0);
  140.      }
  141.       bool = 1;         /* indicate that at least one read was successful */
  142.       switch (*buf) {        /* first char in buffer triggers appropriate function */
  143.     case 'A' : autolog(user,pswrd);
  144.            break;
  145.     case 'L' : login(user,pswrd);
  146.            break;
  147.     case 'W' : waitfor(user,pswrd);
  148.            break;
  149.     case 'U' : use();
  150.            break;
  151.     case ';' : break;     /* ignore comment lines in data file */
  152.  
  153.     default  : printf("\n\aError: invalid command statment ->%s<-\n",buf);
  154.            break;
  155.     }
  156.       }
  157. }
  158.  
  159. /***************************************************************************
  160. *  CheckForLAN() - Check for the presence of the LANtastic redirector.
  161. *
  162. *  Called by:    main()
  163. *
  164. *  Calls:    nothing
  165. *
  166. *  Returns:    1 if LANtastic is detected
  167. *        0 if LAN is not present
  168. */
  169. int CheckForLAN()
  170. {
  171.     /* set up registers and do interrupt (int 2fh, funct. B800h) */
  172.     memset(&rg, '\0', sizeof(rg));
  173.     rg.x.ax=0xb800;
  174.     int86(0x2f,&rg,&rg);
  175.     if (rg.h.al)
  176.     return(1);      /* if AL non-zero => LAN is there */
  177.     return(0);
  178.  
  179. }
  180.  
  181.  
  182. /****************************************************************************
  183. *****************************************************************************
  184. *  Login to a server functions section:
  185. *  Functions defined:
  186. *      autolog() -  Turns the LANtastic Autologin feature on or off.
  187. *      login() -    login with or without specified adapter number.
  188. *      waitfor() -  login waiting for server if there is no initial response.
  189. *      TryLogin() - actual function that does tries to log you in.
  190. *      DoLogin() -  function that sets up registers and does DOS interrupt.
  191. *      ParseLoginLine() -
  192. *            scans the current line of the data file for correct input.
  193. *      ESC_hit() -  Used in waitfor function, checks to see if user hits ESC.
  194. *      delay() -    puts program in busy-wait loop for a given number of secs.
  195. */
  196.  
  197. /***************************************************************************
  198. * autolog() - Turn the LANtastic Auto-login feature on or off
  199. *
  200. *   Called by:    main()
  201. *
  202. *   Calls:    nothing
  203. *
  204. *   Returns:    1 if AUTO statment is not formatted correctly
  205. */
  206. autolog(user,password)
  207. char *user, *password;
  208. {
  209.     char OnOrOff[17];           /* a place to scan in the on or off command */
  210.     char NameAndPassword[36]; /* buffer to put username and password in     */
  211.     int pos;               /* index to keep track of a string position */
  212.  
  213.     /* make sure the AUTO statement is formatted correctly */
  214.     if (sscanf(buf, "AUTO %s%s", OnOrOff, garb) != 1) {
  215.      printf("\n\aWarning: Badly formatted AUTO statement ->%s<-", buf);
  216.      return(1);
  217.      }
  218.  
  219.     /* clear out the buffer for the name and password.    Note that this will
  220.        also automatically set us up for disabling auto-login as the "string"
  221.        needed for the interrupt in that case is "<0><0>".          */
  222.  
  223.        memset(NameAndPassword, '\0', sizeof(NameAndPassword));
  224.  
  225.     /* make sure the either the string 'ON' or 'OFF' was on the AUTO line */
  226.     if (strcmp(OnOrOff, "ON") == 0) {
  227.     strcpy(NameAndPassword, user);        /* if auto-login is being turned */
  228.     pos = strlen(NameAndPassword);        /*    on, then parse the buffer    */
  229.     NameAndPassword[pos]='*';        /*    needed for the interrupt.    */
  230.     strcat(NameAndPassword, password);  /*    it looks like:             */
  231.     NameAndPassword[pos]='\0';        /*    "username<0>password<0>"     */
  232.     /* alert the user that auto-login is being turned on */
  233.     printf("\nLANtastic Auto-login enabled with username %s",user);
  234.     }
  235.     /* if it wasn't 'ON' make sure it is 'OFF' or we have a bad statement */
  236.     else if (strcmp(OnOrOff, "OFF") != 0) {
  237.     printf("\n\aWarning: Badly formatted AUTO statement ->%s<-", buf);
  238.     return(1);
  239.     }
  240.     else printf("\nLANtastic Auto-login feature disabled");
  241.  
  242.     /* set up registers and do interrupt (int 21h, funct. 5FB6h) */
  243.     rg.x.ax=0x5fb6;
  244.     rg.x.di=(unsigned int)NameAndPassword;
  245.     rg.h.bl=0xff;
  246.     segread(&sg);
  247.     sg.es=sg.ds;
  248.     intdosx(&rg,&rg,&sg);
  249.  
  250. }
  251.  
  252.  
  253. /***************************************************************************
  254. * login() - Make one attempt to log into a server
  255. *
  256. *   Called by:    main()
  257. *
  258. *   Calls:    ParseLoginLine(), TryLogin()
  259. *
  260. *   Returns:    1 if login fails or LOGIN statement was badly formatted
  261. *        0 if successful
  262. */
  263. login(user,pswrd)
  264. char *user, *pswrd;
  265. {
  266. char loginString[52];
  267. int  ad_no;
  268.  
  269.    ad_no=MAX_ADAPTERS;       /* initialize adapter number */
  270.  
  271.    /* Parse the login line from the data file, return error if parse fails */
  272.    if (ParseLoginLine(loginString,server,user,pswrd,&ad_no) == 1)
  273.       return (1);
  274.  
  275.    if (TryLogin(loginString,ad_no))
  276.       return(1);
  277.  
  278.    return(0);
  279. }
  280.  
  281.  
  282. /***************************************************************************
  283. * waitfor() - Attempt to login to a server until successful or user cancels
  284. *          retrys.
  285. *
  286. *   Called by:    main()
  287. *
  288. *   Calls:    ParseLoginLine(), TryLogin(), ESC_hit, DoDelay().
  289. *
  290. *   Returns:    1 for any error condition
  291. *        0 if successful
  292. */
  293. waitfor(user,pswrd)
  294. char *user, *pswrd;
  295. {
  296. char loginString[52];
  297. int  ad_no;
  298.  
  299.    ad_no=MAX_ADAPTERS;       /* initialize adapter number */
  300.    WaitFlag = 1;       /* indicate login wait mode    */
  301.  
  302.    /* Parse the login line from the data file, return error if parse fails */
  303.    if (ParseLoginLine(loginString,server,user,pswrd,&ad_no) == 1)
  304.       return (1);
  305.  
  306.    if (TryLogin(loginString,ad_no) != 0) {  /* try login & return if it works */
  307.  
  308.        /* give user the cancel message */
  309.        printf("\nTrying to login to server %-s. Press ESC to cancel retrys...", server);
  310.  
  311.        while (TryLogin(loginString,ad_no))   /* keep trying to log in until:   */
  312.      if (ESC_hit()) {             /* ESC is hit or login succeeds    */
  313.         printf("\nLogin retrys to server %-s cancelled.",server);
  314.         return(1);                 /* return failure            */
  315.         }
  316.      else
  317.         DoDelay(PAUSE_LENGTH);   /* delay PAUSE_LENGTH seconds and try again */
  318.        }
  319.  
  320.    WaitFlag = 0;            /* clear the wait flag */
  321.    return(0);                /* return success */
  322. }
  323.  
  324.  
  325. /****************************************************************************
  326. *  ParseLoginLine: scan the current login line for correct format and
  327. *  information.  Returns error if either is incorrect.
  328. *
  329. *   Called by:    login(), waitfor().
  330. *
  331. *   Calls:    nothing
  332. *
  333. *   Returns:    1 for any error condition
  334. *        0 if successful
  335. *
  336. */
  337. ParseLoginLine(loginString,server,user,pswrd,ad_no)
  338. char *loginString, *server, *user, *pswrd;
  339. int *ad_no;
  340. {
  341.  
  342. int pos, pos1;
  343.  
  344.    /* scan for server name and specified adapter number, if present */
  345.    if (*buf == 'L') {
  346.       if (sscanf(buf, "LOGIN %s%s", server, garb) != 1)
  347.       if (sscanf(buf, "LOGIN %s%d", server, ad_no) != 2) {
  348.     printf("\n\aWarning: Badly formatted LOGIN statement ->%s<-", buf);
  349.         return(1);
  350.         }
  351.       }
  352.    else {
  353.       if (sscanf(buf, "WAITFOR %s%s", server, garb) !=1)
  354.      if (sscanf(buf, "WAITFOR %s%d", server, ad_no) != 2) {
  355.      printf("\n\aWarning: Badly formatted WAITFOR statement ->%s<-", buf);
  356.      return(1);
  357.      }
  358.       }
  359.  
  360.    /* check for valid adapter number.  MAX_ADAPTERS indicates none specified */
  361.    if (*ad_no < 0 || *ad_no > MAX_ADAPTERS) {
  362.       printf("\n\aWarning: Invalid adapter number specified ->%s<-",buf);
  363.       return(1);
  364.       }
  365.  
  366.    /* set up login string. it looks like: "\\server\user<0>password<0>" */
  367.    strcpy(loginString,"\\\\");
  368.    strcat(loginString,server);
  369.    strcat(loginString,"\\");
  370.    strcat(loginString,user);
  371.    pos=strlen(loginString);
  372.    loginString[pos]='*';
  373.    loginString[pos+1]='\0';
  374.    strcat(loginString,pswrd);
  375.    pos1=strlen(loginString);
  376.    loginString[pos]=0;
  377.    loginString[pos1+1]=0;
  378.    return(0);
  379.  }
  380.  
  381.  
  382. /****************************************************************************
  383. *   TryLogin(loginString, ad_no)
  384. *   Using the loginString and adapter number (ad_no), attempt a login.
  385. *    If the adapter number is not specified, attempt login on all adapters.
  386. *
  387. *   Called by:    login(), waitfor().
  388. *
  389. *   Calls:    DoLogin(), geterr().
  390. *
  391. *   Returns:    1 if login fails
  392. *        0 if login works
  393. *
  394. */
  395. TryLogin(loginString,ad_no)
  396. char *loginString;
  397. int  ad_no;
  398. {
  399. int i;
  400.  
  401.    i=ad_no;        /* set i to adapter number for use in 'logged in' message */
  402.  
  403.    /* if ad_no == MAX_ADAPTERS => no ad. no. specified, try login on all possible adapters */
  404.    if (ad_no == MAX_ADAPTERS)
  405.       for (i=0; i < MAX_ADAPTERS; i++) {
  406.      DoLogin(loginString,i);
  407.      if (rg.x.cflag && rg.x.ax == 0x35)    /* break if error is other than    */
  408.         ;                   /* "Cannot locate network name" */
  409.      else
  410.         break;
  411.      }
  412.    else DoLogin(loginString,ad_no);
  413.  
  414.    if (!rg.x.cflag)
  415.       printf("\nLogged into: %-s on adapter %d",server,i);
  416.    else {
  417.       if (WaitFlag)
  418.      return(1);
  419.       get_err();
  420.       printf("\nError logging into %-s -> %s",server,err);
  421.       return(1);
  422.       }
  423.  
  424.    return(0);
  425. }
  426.  
  427.  
  428. /***************************************************************************
  429. *   DoLogin(LoginString,ad_no)
  430. *
  431. *   Set up the registers and do the dos interrupt to login to a server using
  432. *    the provided login string and adapter number.
  433. *
  434. *   Called by:    TryLogin().
  435. *
  436. *   Calls:    nothing
  437. *
  438. *   returns:    nothing, but modifies the global register and segment variables
  439. *
  440. */
  441. DoLogin(LoginString,ad_no)
  442. char *LoginString;
  443. int ad_no;
  444. {
  445.    rg.x.ax=0x5f81;
  446.    rg.x.di=(unsigned int)LoginString;
  447.    rg.h.bl=ad_no;
  448.    segread(&sg);
  449.    sg.es=sg.ds;
  450.    intdosx(&rg,&rg,&sg);
  451.  
  452. }
  453.  
  454.  
  455. /****************************************************************************
  456. *   ESC_hit() - check to see if the user has hit the ESC key.
  457. *
  458. *   Called by:    waitfor()
  459. *
  460. *   Calls:    nothing
  461. *
  462. *   Returns:    1 if ESC was pressed
  463. *        0 if not
  464. *
  465. */
  466. ESC_hit()
  467. {
  468.  
  469.    /* see if the user has hit the ESC key to cancel wait */
  470.    if (_kbhit())
  471.       if (_getch() == ESC_CHAR)
  472.     return(1);
  473.  
  474.    return(0);
  475.  
  476. }
  477.  
  478. /****************************************************************************
  479. * DoDelay(DelayLen) - delay for the amount of time specified by DelayLen.
  480. *
  481. *   Called by:    waitfor().
  482. *
  483. *   Calls:    nothing
  484. *
  485. *   Returns:    nothing
  486. *
  487. */
  488. DoDelay(DelayLen)
  489. int DelayLen;
  490. {
  491.    time_t CurrentTime, EndTime;
  492.  
  493.    time(&CurrentTime);            /* get the current time     */
  494.    EndTime = CurrentTime + DelayLen;    /* calculate the ending time */
  495.    while (CurrentTime < EndTime)    /* keep checking the time   */
  496.       time(&CurrentTime);        /*  until we're there       */
  497.  
  498. }
  499.  
  500. /***************************************************************************
  501. ****************************************************************************
  502. *  Use drives and printers section
  503. *  Functions defined:
  504. *    use() -     Attempt to redirect the drive or printer specified
  505. *
  506. ****************************************************************************/
  507.  
  508. /****************************************************************************
  509. * use() - Attempt to redirect a device
  510. *
  511. *   Called by:    main()
  512. *
  513. *   Calls:    geterr()
  514. *
  515. *   Returns:    1 if login fails or LOGIN string is badly formatted
  516. *        0 if successful
  517. *
  518. */
  519. use()
  520. {
  521. char locname[17], netname[129];
  522. int type;
  523.  
  524.    /* get local device to redirect and network device name */
  525.    if (sscanf(buf, "USE %s%s%s", locname, netname, garb) != 2) {
  526.       printf("\n\aWarning: Badly fomatted USE statement ->%s<-", buf);
  527.       return(1);
  528.       }
  529.  
  530.    /* decide type of use, set up registers, do interrupt */
  531.    if (strncmp(locname, "PRN", 3) == 0 || strncmp(locname, "LPT", 3) == 0 || strncmp(locname,"COM", 3) == 0)
  532.       type = PRINTER;
  533.    else
  534.       type = DRIVE;
  535.  
  536.    rg.x.ax=0x5f03;
  537.    rg.h.bl=type;
  538.    rg.x.cx=0;
  539.    rg.x.si=(unsigned int)locname;
  540.    rg.x.di=(unsigned int)netname;
  541.    segread(&sg);
  542.    sg.es=sg.ds;
  543.    intdosx(&rg,&rg,&sg);
  544.  
  545.    /* if carry flag set == error, give message  */
  546.    if (!rg.x.cflag)
  547.       printf("\nDevice %-s redirected to %-s ",locname, netname);
  548.    else {
  549.       get_err();
  550.       printf("\nError redirecting %-s -> %s",locname,err);
  551.       return(1);
  552.       }
  553.  
  554.    return(0);
  555.  
  556. }
  557.  
  558. /***************************************************************************
  559. ****************************************************************************
  560. *
  561. *   Utilities section - various general functions used by any other function
  562. *
  563. *   Contains:    geterr(), towupper(), fillbuf().
  564. *
  565. ****************************************************************************/
  566.  
  567. /* get_err() - Expand LANOS error code to string */
  568. get_err()
  569. {
  570.    /* set up the registers */
  571.    rg.x.bx = rg.x.ax;
  572.    rg.h.al = (unsigned char)rg.x.ax;
  573.    rg.h.ah = 5;                         /* 5 = LANOS funct # to expand error number */
  574.    segread(&sg);
  575.    sg.es=sg.ds;
  576.    int86x(0x2f,&rg,&rg,&sg);
  577.  
  578.    /* ES:DI now points to a read only string containing expanded error message */
  579.    FP_SEG(error) = sg.es;
  580.    FP_OFF(error) = rg.x.di;
  581.    eptr = error;
  582.    bptr = err;
  583.    do {                 /* read system string to local buffer */
  584.       *bptr = *eptr;
  585.       ++bptr;
  586.       ++eptr;
  587.       } while (*eptr != '\0');
  588.    *bptr = '\0';
  589. }
  590.  
  591.  
  592. /* convert entire word to uppercase  */
  593. towupper(wrd)
  594. char *wrd;
  595. {
  596.    do {
  597.    *wrd = toupper(*wrd);
  598.    ++wrd;
  599.       } while (*wrd != '\0');
  600.  
  601. }
  602.  
  603.  
  604. /* fill_buf - read a line from user's data file into global BUF
  605.       returns: 0 - if EOF encountered
  606.            1 - if buffer was filled                */
  607. int fill_buf()
  608. {
  609.    int ch, pos;
  610.  
  611.    /* initialize buffer */
  612.    for (pos = 0; pos <= sizeof(buf); pos++)
  613.       buf[pos] = '\0';
  614.  
  615.    /* skip white space */
  616.    do {
  617.       ch = fgetc(fp);
  618.       } while (isspace(ch));
  619.    if (ch == EOF)
  620.      return 0;
  621.  
  622.    pos = 0;
  623.    while (ch != '\n' && ch != EOF) {
  624.       buf[pos++] = ch;
  625.       ch = fgetc(fp);
  626.       }
  627.    towupper(buf);
  628.    return 1;
  629. }
  630.